#include "protocol.h"

int main()
{
    int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket()");
        exit(1);
    }

    struct sockaddr_un addr;

    addr.sun_family = AF_UNIX;
    unlink(SERVERPATH);
    strcpy(addr.sun_path, SERVERPATH);

    if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind()");
        exit(1);
    }
    
    struct sockaddr_un dstaddr;
    socklen_t len = sizeof(dstaddr);
    char recvbuff[BUFFSIZE] = {0};
    while (1)
    {
        recvfrom(sockfd, recvbuff, BUFFSIZE, 0, (struct sockaddr *)&dstaddr, &len);
        printf("Data from: %s\n", dstaddr.sun_path);
        puts(recvbuff);
        sendto(sockfd, "recv over!", 10, 0, (struct sockaddr *)&dstaddr, len);
    }

    close(sockfd);
}
